<?php
// +----------------------------------------------------------------------
// | ManniuRead
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://manniuyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: heitao <read@manniuyun.com>
// +----------------------------------------------------------------------

namespace app\admin\model;

use manniu\model\Base;

class Navigator extends Base
{
    // 导航id集
    public $ids = [];

    //自定义初始化
    protected function initialize(){
        parent::initialize();
    }
	
	/**
     * 树形结构 - select
     * @access public
     * @param int $pid          父栏目ID
     * @param int $build_url    option的value设置为url，默认为栏目ID
     * @return string
     */
    public function getTreeOption($pid = 0) {
    	$pid = (int)$pid;
    	// 查询所有内容
        $result = $this->order(['displayorder'=> 'asc','id'=>'desc'])->select();
        // 生成树型类需要的数组
        $list = [];
        foreach ($result as $key => $value) {
            $list[$key] = $value->toArray();
            $list[$key]['selected'] = $value->id == $pid ? 'selected' : '';
        }
        // 生成树型结构的基本代码
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        // 实例化树型类
        $tree = new \manniu\tools\Tree;
        //设置树型结构所需的数组
        $tree->init($list);
        $data = $tree->getTree(0, $str);
        return $data ? $data : '';
    }

    /**
     * 树形结构 - table
     * @access public
     * @param int $pid  父栏目ID
     * @return string
     */
    public function getTreeTable($pid = 0) {
    	$pid = (int)$pid;
    	// 查询所有内容
        $result = $this->order(['displayorder'=> 'asc','id'=>'desc'])->select();
        // 实例化树型类
        $tree = new \manniu\tools\Tree;
        // 设置初始值
        $tree->icon = ['&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        // 生成树型类需要的数组
        $list = [];
        if($result){
            foreach ($result as $key => $value) {
                $list[$key] = $value->toArray();
                $list[$key]['show'] = '';
                $list[$key]['show'] .= '<i class="fa ';
                $list[$key]['show'] .= $value->getAttr('show') ? 'fa-check-circle color_primary' : 'fa-times-circle color_danger';
                $list[$key]['show'] .= '" style="font-size:18px;">';

                $list[$key]['target'] = '';
                $list[$key]['target'] .= '<i class="fa ';
                $list[$key]['target'] .= $value->getAttr('target') ? 'fa-check-circle color_primary' : 'fa-times-circle color_danger';
                $list[$key]['target'] .= '" style="font-size:18px;">';

                $list[$key]['mn_option'] = '<div class="layui-btn-group">';
                $list[$key]['mn_option'] .= '<a class="layui-btn layui-btn-small layui-btn-primary" href="'.url('add',['pid' =>$value->getAttr('id')]).'"><i class="layui-icon">&#xe654;</i> 添加子类</a>';
                $list[$key]['mn_option'] .= '<a class="layui-btn layui-btn-small layui-btn-primary" href="'.url('edit',['id' =>$value->getAttr('id')]).'"><i class="layui-icon">&#xe642;</i> 修改</a>';
                $list[$key]['mn_option'] .= '<a class="layui-btn layui-btn-small layui-btn-primary" href="javascript:;"  onclick="mn_confirm_url(\'确定删除 【'.$value->getAttr('name').'】 吗？\',\''.url('del',['id' =>$value->getAttr('id')]).'\')"><i class="layui-icon">&#xe640;</i> 删除</a>';
                $list[$key]['mn_option'] .= '</div>';
            }
        }
        //设置树型结构所需的数组
        $tree->init($list);
        // 生成树型结构的基本代码
        $str = "<tr>
                    <td class='text-center'><input type='checkbox' name='ids[]' lay-skin='primary' value='\$id' /></td>
                    <td class='text-center'><input type='text' class='layui-input displayorder' name='data[\$id][displayorder]' autocomplete='off' value='\$displayorder'></td>
                    <td>\$spacer\$name</td>
                    <td>\$url</td>
                    <td class='text-center'>\$show</td>
                    <td class='text-center'>\$target</td>
                    <td>\$mn_option</td>
                </tr>";
        $data = $tree->getTree($pid, $str);
        return $data ? $data : '';
    }

    /**
     * 树形结构 - array
     * @access public
     * @param int $pid  父栏目ID
     * @return array
     */
    public function getTreeArray($pid = 0) {
        $pid = (int)$pid;
        // 查询所有内容
        $result = $this->order(['displayorder'=> 'asc','id'=>'desc'])->select();
        // 实例化树型类
        $tree = new \manniu\tools\Tree;
        //设置树型结构所需的数组
        $tree->init(collection($result)->toArray());
        $data = $tree->getTreeArray($pid);
        return $data ? $data : [];
    }

    /**
     * 删除单页
     * @param   array   $ids    单页id数组
     */
    public function delNav($ids) {
        if (is_array($ids)) {
            foreach ($ids as $id) {
                $this->getId($id);
            }
        } else {
            $this->getId($ids);
        }
        
        $this->ids && $this->where('id','in',$this->ids)->delete();
    }

    /**
     * 获取自己id和子id
     */
    private function getId($id) {

        if (!$id) {
            return NULL;
        }

        $this->ids[$id] = (int)$id;

        $data = $this->where('pid', $id)->field('id')->select();
        if (!$data) {
            return NULL;
        }

        foreach ($data as $t) {
            $this->ids[$t->getAttr('id')] = $t->getAttr('id');
            $this->getId($t->getAttr('id'));
        }
    }
}